package by.gusto.afisha.db;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import by.gusto.afisha.R;
import by.gusto.afisha.model.Category;
import by.gusto.afisha.model.Place;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "gusto.db";
	private static final int DATABASE_VERSION = 6;
	private static final String TAG = DatabaseHelper.class.getName();
	private Dao<Category, String> mCategoryDao;
	private Dao<Place, String> mPlaceDao;

	public DatabaseHelper(final Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * This is called when the database is first created.
	 */
	@Override
	public void onCreate(final SQLiteDatabase db, final ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, Category.class);
			TableUtils.createTable(connectionSource, Place.class);
			
//			fillTestData();
		} catch (java.sql.SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * This is called when your application is upgraded and it has a higher version number.
	 */
	@Override
	public void onUpgrade(final SQLiteDatabase db, final ConnectionSource connectionSource, final int oldVersion, final int newVersion) {
		try {
			// after we drop the old databases, we create the new ones
			TableUtils.dropTable(connectionSource, Category.class, true);
			TableUtils.dropTable(connectionSource, Place.class, true);
			onCreate(db, connectionSource);
			
		} catch (java.sql.SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}
	
	private void fillTestData() {
		try {
			mCategoryDao = getCategoryDAO();
			mCategoryDao.create(new Category("category 1"));
			mCategoryDao.create(new Category("category 2"));
			
			mPlaceDao = getPlaceDAO();
			mPlaceDao.create(new Place("c2 - 1", 1));
			mPlaceDao.create(new Place("c2 - 2", 1));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Dao<Category, String> getCategoryDAO() throws SQLException{
		if (mCategoryDao == null) {
			try {
				mCategoryDao = DaoManager.createDao(connectionSource, Category.class);
			} catch (java.sql.SQLException e) {
				Log.e(TAG, "error creating Category DAO in " + DATABASE_NAME);
				throw new RuntimeException();
			}
		}
		return mCategoryDao;
	}
	
	public Dao<Place, String> getPlaceDAO() throws SQLException{
		if (mPlaceDao == null) {
			try {
				mPlaceDao = DaoManager.createDao(connectionSource, Place.class);
			} catch (java.sql.SQLException e) {
				Log.e(TAG, "error creating Category DAO in " + DATABASE_NAME);
				throw new RuntimeException();
			}
		}
		return mPlaceDao;
	}
	
	@Override
	public void close() {
	    super.close();
	    mCategoryDao = null;
	    mPlaceDao = null;
	}
	
}
